<?php

/*
user token
yangchao
*/

if (isset($_GET["test_db"]))
{
	include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
}

class db_user_token extends db_base
{
	var $tbl_name = "user_token";

	function db_user_token()
	{
		parent::db_base();
	}

		
	//添加一条数据
	function insert_user_token(
		$uid,
		$appkey,
		$access_token,
		$version,
		$expires_in,
		$refresh_token="",
		$remind_in=""
		
	)
	{
		global $config;
		//详细字段
		$ary = array();
		$ary["uid"] = $uid;
		$ary["appkey"] = $appkey;
		$ary["access_token"] = $access_token;
		$ary["refresh_token"] = $refresh_token;
		$ary["version"] = $version;
		$ary["expires_in"] = $expires_in;
		$ary["remind_in"] = $remind_in;
		$ary["create_time"] = Date::get_date_time();
		
		$ret = $this->insert($this->tbl_name, $ary,"",true);
		
		
		$mc = mem_cache::get_instance();
		
		//更新列表
		$key[] = CACHE_USER_TOKEN . "-get_token_by_uid_and_appkey-{$uid}-{$appkey}-{$version}";
		$key[] = CACHE_USER_TOKEN . "-get_token_by_uid_or_appkey-{$uid}-{$appkey}-{$version}-1-1";
		$key[] = CACHE_USER_TOKEN . "-get_token_by_uid_or_appkey-0-{$appkey}-{$version}-1-20";
		$key[] = CACHE_USER_TOKEN . "-get_token_count_by_uid_or_appkey-0-{$appkey}-{$version}";
		$mc->delete_ary($key);
		
		
		if ($ret)
		{
			return true;
		}
		else
		{
			return false;
		}

		return true;
	}

	//根据uid和appkey取token	
	function get_token_by_uid_and_appkey($uid,$appkey,$version=2)
	{
		if (!cgi_chk::uid($uid))
		{
			$user_info = new user_info();
			$user_item = $user_info->get_user_info_by_uid($uid);
			
			
			$uid = $user_item["openid"];
		}
		
		
		
		
		// SQL注入检查
		if (!cgi_chk::uid($uid))
		{
			throw new Exception("DB:uid 不正确");
		}
		$key = CACHE_USER_TOKEN . "-get_token_by_uid_and_appkey-{$uid}-{$appkey}-{$version}";
		$mc = mem_cache::get_instance();
		
		// 先获取cache
		$cache = array();
		$cache = $mc->get($key);
		$uid = intval($uid);
		$appkey = intval($appkey);
		$version = intval($version);
		
		$where = " uid = '{$uid}'  and  appkey = '{$appkey}' and version = {$version} ";
		
		if ($cache === false)
		{
			$content = $this->get_alllist($this->tbl_name, 1, 1, "", $where);
			// 保存cache
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_10);
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content[0];
	}
	
	//获取用户token,谨慎用，严禁JS调用和其他外泄
	function get_token_list_by_uid_or_appkey($uid="",$appkey="",$version=2,$p=1,$pcount=10)
	{
		// SQL注入检查
		if ( $uid && !cgi_chk::uid($uid))
		{
			throw new Exception("DB:uid 不正确");
		}
		
		if(!$uid && !$appkey )
		{
			throw new Exception("uid和appkey不能全为空");
		}
		$key = CACHE_USER_TOKEN . "-get_token_by_uid_or_appkey-{$uid}-{$appkey}-{$version}-{$p}-{$pcount}";
		$mc = mem_cache::get_instance();
		
		// 先获取cache
		$cache = array();
		$cache = $mc->get($key);
		
		$uid = intval($uid);
		$appkey = intval($appkey);
		$version = intval($version);
		
		if($uid)$where_arr[] = " uid = '{$uid}'  ";
		if($appkey)$where_arr[] = " appkey = '{$appkey}'  ";
		$where_arr[] = " version = {$version} ";
		$where =  join(" and ",$where_arr);
		
		if ($cache === false)
		{
			$content = $this->get_alllist($this->tbl_name, $p, $pcount, "", $where);
			// 保存cache
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_1);
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content;
	}
	
	
	function get_token_count_by_uid_or_appkey($uid="",$appkey="",$version=2)
	{
		// SQL注入检查
		if ( $uid && !cgi_chk::uid($uid))
		{
			throw new Exception("DB:uid 不正确");
		}
		
		if(!$uid && !$appkey )
		{
			throw new Exception("uid和appkey不能全为空");
		}
		
		$uid = intval($uid);
		$appkey = intval($appkey);
		$version = intval($version);
		
		
		$key = CACHE_USER_TOKEN . "-get_token_count_by_uid_or_appkey-{$uid}-{$appkey}-{$version}";
		$mc = mem_cache::get_instance();
		
		// 先获取cache
		$cache = array();
		$cache = $mc->get($key);
		
		if($uid)$where_arr[] = " uid = '{$uid}'  ";
		if($appkey)$where_arr[] = " appkey = '{$appkey}'  ";
		$where_arr[] = " version = {$version} ";
		$where =  join(" and ",$where_arr);
		
		if ($cache === false)
		{
			$content = $this->get_listcount($this->tbl_name, $where);
			// 保存cache
			$mc->set($key, serialize($content), 0, CACHE_EXPIRE_MIN_1);
		}
		else
		{
			$content = unserialize($cache);
		}
		return $content;
	}
	
/*
 * 取 调用接口列表
 */
	function admin_get_list_by_uid_and_version($uid,$version)
	{
		$where_array = array();
		if($uid)
		{
			$where_array[] = "uid = {$uid}";
		} 
		
		if($$version)
		{
			$where_array[] = "version = {$version}";
		} 
		
		$where = join(" and " ,$where_array);
		
		if(!$where)
		{
			$where = 1;
		}
		
		$res = $this->get_alllist($this->tbl_name,1,1000,"create_time desc",$where);
		
		
		return $res;
	}
	
	/*
	 * 取 调用接口总数
	 */
	function admin_get_count_by_search($site_id="",$api="",$create_time="",$end_time="",$refer="",$appkey=0,$error="")
	{
		$where_array = array();
		if($site_id)
		{
			$where_array[] = "site_id = {$site_id}";
		} 
		if($api)
		{
			$where_array[] = "api like '{$api}%'";
		} 
		if($end_time)
		{
			$where_array[] = "create_time < '{$end_time}'";
		} 
		if($create_time)
		{
			$where_array[] = "create_time > '{$create_time}'";
		} 
		if($refer)
		{
			$where_array[] = "refer like '{$refer}%'";
		} 
		if($appkey)
		{
			$where_array[] = "appkey = {$appkey}";
		} 
		if($error)
		{
			$where_array[] = "error > {$error}";
		} 
		$where = join(" and " ,$where_array);
		
		$res = $this->get_listcount($this->tbl_name,$where);
		
		return $res;
	}
	
}


